package com.houtai.controller.campus;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.bean.dto.article.CampusArticleListDto;
import com.houtai.bean.dto.campus.CampusDetailDto;
import com.houtai.bean.dto.campus.CampusListDto;
import com.houtai.bean.dto.campus.TdCampusMapListDto;
import com.houtai.bean.dto.company.CompanyListDto;
import com.houtai.bean.vo.article.CampusArticleListVo;
import com.houtai.bean.vo.brand.TdBrandListResult;
import com.houtai.bean.vo.campus.CampusDetailVo;
import com.houtai.bean.vo.campus.CampusListVo;
import com.houtai.bean.vo.campus.CampusTopListVo;
import com.houtai.bean.vo.campus.TdCampusMapListVo;
import com.houtai.bean.vo.company.CompanyListVo;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.dict.DictResult;
import com.houtai.service.campus.CampusService;
import com.houtai.service.company.CompanyService;
import com.houtai.service.dict.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="园区接口", tags={"园区接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class CampusContorller {
    @Autowired
    private DictService dictService;

    @Autowired
    private CampusService campusService;
    @Autowired
    private CompanyService companyService;
    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="园区类别列表", notes="园区类别列表")
    @RequestMapping(value = "/campus/type/list", method = RequestMethod.POST)
    public DictListResult type() {
        return dictService.findDictByType("campus_type");
    }
    @ApiOperation(value="园区列表", notes="园区列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/list")
    public CampusListVo list(@ApiParam(value = "园区列表所需参数") @Valid @RequestBody CampusListDto dto, BindingResult result) {
        CampusListVo retInfo = new CampusListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = campusService.campusList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="园区详情", notes="园区详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/detail")
    public CampusDetailVo detail(@ApiParam(value = "园区详情所需参数") @Valid @RequestBody CampusDetailDto dto, BindingResult result,
                                 @RequestHeader String token) {
        CampusDetailVo retInfo = new CampusDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = campusService.campusDetail(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="园区企业列表", notes="园区企业列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/company/list")
    public CompanyListVo list(@ApiParam(value = "园区企业列表所需参数") @Valid @RequestBody CompanyListDto dto, BindingResult result) {
        CompanyListVo retInfo = new CompanyListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = companyService.companyList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="园区文章列表", notes="园区文章列表（园区动态 园区活动）")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/article/list")
    public CampusArticleListVo list(@ApiParam(value = "园园区文章列表所需参数") @Valid @RequestBody CampusArticleListDto dto, BindingResult result) {
        CampusArticleListVo retInfo = new CampusArticleListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = campusService.articleList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="园区榜单前十", notes="园区榜单前十")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/top/ten")
    public CampusTopListVo top() {
        return campusService.campusTopList();
    }
    @ApiOperation(value="园区指数前十", notes="园区指数前十")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/index/ten")
    public CampusTopListVo index() {
        return campusService.campusIndexList();
    }
    @ApiOperation(value="品牌榜前十", notes="品牌榜前十")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/brand/top/ten")
    public TdBrandListResult brand() {
        return campusService.tdBrandList();
    }



    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="获取当前城市", notes="获取当前城市")
    @RequestMapping(value = "/city/current", method = RequestMethod.POST)
    public DictResult city(HttpServletRequest request) {
        return dictService.findDictByIp(request);
    }

    @ApiOperation(value="地图园区列表", notes="地图园区列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/campus/map/list")
    public TdCampusMapListVo list(@ApiParam(value = "地图园区列表所需参数") @Valid @RequestBody TdCampusMapListDto dto, BindingResult result) {
        TdCampusMapListVo retInfo = new TdCampusMapListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = campusService.campusList(dto);
        }
        return retInfo;
    }
}
